package com.wontube.smr.bo.sites.crunchyroll;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.bo.constants.StandarAnimeGenres;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.dao.bo.GenreSelectDao;
import com.wontube.smr.web.client.model.po.GenreSite;

/**
 * @author tanggf(2012-8-1)
 *
 */
public final class CrunchyrollAnimeGenres {
	private CrunchyrollAnimeGenres(){}
	
	private static final Map<String, String> siteStandarMap = new HashMap<String, String>();	
	private static Map<String, String> map = new HashMap<String, String>();
	
	static {
		loadAnimeGenersMap();
		
		//{"action", "adventure", "comedy", "drama", "ecchi", "fantasy", "historical", "mecha", "romance", "science_fiction", "seinen", "shoujo", "shounen", "slice_of_life", "sports"};
		map.put("action", StandarAnimeGenres.ACTION);
		map.put("adventure", StandarAnimeGenres.ADVENTURE);
		map.put("comedy", StandarAnimeGenres.COMEDY);
		map.put("drama", StandarAnimeGenres.DRAMA);
		map.put("ecchi", StandarAnimeGenres.ECCHI);
		map.put("fantasy", StandarAnimeGenres.FANTASY);
		map.put("historical", StandarAnimeGenres.HISTORICAL);		
		map.put("mecha", StandarAnimeGenres.MECHA);
		map.put("romance", StandarAnimeGenres.ROMANCE);
		map.put("science_fiction", StandarAnimeGenres.SCIENCE_FICTION);
		map.put("seinen", StandarAnimeGenres.MATURE);
		map.put("shoujo", StandarAnimeGenres.SHOUJO);
		map.put("shounen", StandarAnimeGenres.SHOUNEN);
		map.put("slice_of_life", StandarAnimeGenres.SLICE_OF_LIFE);
		map.put("sports", StandarAnimeGenres.SPORTS);		
	}
	
	private static void loadAnimeGenersMap(){
		List<GenreSite> genreSites = GenreSelectDao.selectGenreSites(SiteConstants.WEBSITE_CRUNCHYROLL,
				Genre.CATE_ANIME);
		for (GenreSite gs : genreSites) {
			siteStandarMap.put(gs.getSiteGenre(), gs.getStandarGenre());
		}
	}
	
	/**
	 * 修正单个genre
	 * @param genreName
	 * @return
	 */
	public static String remendGenre(String genreName){
		if(StringUtils.isNullOrEmpty(genreName)){
			return null;
		}
		
		//处理专有genre, 不存在则判断是标准genre
		genreName = genreName.trim();
		String genre = map.get(genreName);
		return genre != null ? genre : genreName;
	}
	
	/**
	 * 处理全部genre,并防止重复
	 * @param genreNames (逗号分隔)
	 * @return
	 */
	public static String remendAllGenres(String genreNames){
		if(StringUtils.isNullOrEmpty(genreNames)){
			return null;
		}
		
		Set<String> genreSet = new HashSet<String>();		
		String[] genres = genreNames.split("\\s*,\\s*");
		for (String g : genres) {
			String rg = remendGenre(g);
			if(rg != null){
				genreSet.add(rg);
			}		
		}
		
		return ArrayUtils.join(genreSet, ",");
	}
	
	/**
	 * Crunchyroll 内部使用的genres
	 * @return
	 */
	public static Set<String> getInitAnimeGenres(){
		return map.keySet();
	}
}
